<template>
  <div class="sub-category">
    <div class="container">
      <!-- 面包屑 -->
      <subBread></subBread>
      <!-- 筛选区域 -->
      <subFilter @filter-change="filterChange"></subFilter>
      <!-- 结果区域 -->
      <div class="goods-list">
        <!-- 排序 -->
        <subSort @sort-change="changeSort" ></subSort>
        <!-- 列表 -->
        <ul>
          <li v-for="i in goodsList" :key="i.id">
            <GoodsItem :goods="i" />
          </li>
        </ul>
        <!-- 加载 -->
        <XtxInfiniteLoading :loading="loading" :finished="finished" @infinite="getDate" />
      </div>
    </div>
  </div>
</template>

<script>
import { reactive, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import subBread from '@/views/category/components/sub-bread'
import subFilter from '@/views/category/components/sub-filter'
import subSort from '@/views/category/components/sub-sort'
import GoodsItem from '@/views/category/components/goods-item'
//导入请求接口
import { findSubCategoryGoods } from '@/api/category'
export default {
  name: 'SubCategory',
  components: {
    subBread,
    subFilter,
    subSort,
    GoodsItem
  },
  setup() {
    //定义控制加载的数据
    const loading = ref(false)
    //数据是否加载完毕
    const finished = ref(false)
    //商品列表数据
    const goodsList = ref([])
    //请求参数
    let reqParams = {
      page: 1,
      pageSize: 20
    }
    const route = useRoute()
    const getDate = () => {
      loading.value = true
      reqParams.categoryId = route.params.id
      findSubCategoryGoods(reqParams).then(({ result }) => {

        if (result.items.length) {
          //如果有数据
          goodsList.value.push(...result.items)
          //数据加载完成后把状态改回false
          loading.value = false
          reqParams.page++
        } else {
          //数据加载完毕（没有数据）
          finished.value = true

        }
        loading.value = false
      })
    }
    watch(() => route.params.id, (newValue) => {
      //当地址栏变化时
      if (newValue && `/category/sub/${newValue}` === route.path) {

        //1.清空数据
        goodsList.value = []
        //2.页码值重制
        reqParams = {
          page: 1,
          pageSize: 20
        }
        //3.隐藏加载完毕后的样式
        finished.value = false
      }
    })


    //当更改排序组件的时候
    const changeSort = (sortParams) => {
      //1.清空数据
      goodsList.value = [],
      reqParams = { ...reqParams,...sortParams}
      reqParams.page = 1
      //隐藏加载完毕后的样式
      finished.value = false
    }

    //当更改筛选组件的时候
    const filterChange = (filterParams) => {
      //1.清空数据
      goodsList.value = [],
      reqParams = { ...reqParams,...filterParams}
      reqParams.page = 1
      //隐藏加载完毕后的样式
      finished.value = false
    }

    return {
      getDate,
      finished,
      loading,
      goodsList,
      changeSort,
      filterChange
    }
  }
}
</script>

<style scoped lang='less'>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;

  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;

    li {
      margin-right: 20px;
      margin-bottom: 20px;

      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>